package com.jfdesgagne.traklib.ui {
	import fl.controls.TextArea;
	import fl.motion.easing.Exponential;
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;

	import com.jfdesgagne.traklib.statics.TrakTypes;
	import com.jfdesgagne.traklib.ui.components.TrakUIOptions;
	import com.jfdesgagne.traklib.ui.events.TrakUIEvent;
	import com.yahoo.astra.fl.controls.TabBar;
	import com.yahoo.astra.fl.events.TabBarEvent;

	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;

	//import fl.controls.NumericStepper;

	/**
	 * @author jdesgagne
	 */
	public class TrakUI extends MovieClip {
		public var options_mc:TrakUIOptions;
		
		public var source_mc:MovieClip;
		public var output_mc:MovieClip;
		public var close_mc:MovieClip;
		public var tabBar:TabBar;
		public var output_txt:TextArea;
		
		protected var __openned:Boolean = false;
		protected var __bounds:Shape;
		protected var __tween:Tween;
		protected var __colorDetection:Boolean = false;
		protected var __colorDetected:int;
		protected var __colorDetectionZone:Shape;
		protected var __colorDetectionColor:Shape;
		protected var __colorCountdown:TextField;
		
		
		public function TrakUI() {   
			this.x = -this.width + 26;
			this.y = -30;
			this.addEventListener(Event.ADDED_TO_STAGE, this.init);
		}
		
		public function outputDebug(_text:String):void {
			if(this.output_txt.text!= "") this.output_txt.text += "/n";
			this.output_txt.text += _text;
		}

		protected function init(e:Event):void {
			this.removeEventListener(Event.ADDED_TO_STAGE, this.init);
			this.addEventListener(Event.REMOVED_FROM_STAGE, this.init);
			if(this.__tween) {
				this.__tween.stop();
			}
			this.addEvents();
			this.stage.focus=this.stage;
			this.source_mc.addEventListener(MouseEvent.CLICK, this.onStageClick);
			this.tabBar.addEventListener(TabBarEvent.ITEM_CLICK, this.onTabBarItemClicked);
		}
		
		private function onTabBarItemClicked(e:TabBarEvent):void {
			this.options_mc.trackingType = e.item.data;
			this.dispatchEvent(new TrakUIEvent(TrakUIEvent.TRACKING_CHANGED, e.item.data));
		}

		protected function onRemovedFromStage(e:Event):void {
			this.addEventListener(Event.ADDED_TO_STAGE, this.init);
			this.removeEventListener(Event.REMOVED_FROM_STAGE, this.init);
			this.options_mc.removeEvents();
		}
		
		protected function onStageClick(e:MouseEvent):void {
			this.stage.focus=this.stage;
		}
		
		protected function addEvents():void {
			this.close_mc.buttonMode = true;
			this.close_mc.addEventListener(MouseEvent.CLICK, this.open);
			
		}
		
		public function set trackingTypes(_trackingTypes:Array):void {
			//this.trackingTypeSelector.dataProvider = new DataProvider(_trackingTypes);
		}
		
		public function close(e:MouseEvent=null):void {
			this.close_mc.removeEventListener(MouseEvent.CLICK, this.close);
			this.close_mc.buttonMode = false;
			this.close_mc.gotoAndStop("open");
			this.__tween = new Tween(this, "x", Exponential.easeOut, this.x, -this.width + 28, 1, true);
			this.__tween.addEventListener(TweenEvent.MOTION_FINISH, this.closeStep2);
		}
		
		protected function closeStep2(e:TweenEvent):void {
			this.__tween.removeEventListener(TweenEvent.MOTION_FINISH, this.closeStep2);
			this.__tween = new Tween(this, "y", Exponential.easeInOut, this.y, -26, 1, true);
			this.__tween.addEventListener(TweenEvent.MOTION_FINISH, this.readyToOpen);
		}
		
		protected function readyToOpen(e:TweenEvent):void {
			this.__tween.removeEventListener(TweenEvent.MOTION_FINISH, this.readyToOpen);
			this.__tween = null;
			this.close_mc.addEventListener(MouseEvent.CLICK, this.open);
			this.close_mc.buttonMode = true;
			
			this.dispatchEvent(new TrakUIEvent(TrakUIEvent.CLOSE));
			this.__openned = false;
		}
		
		public function testImage(_image:Bitmap):void {
			trace('addchild');
			_image.y = 500;
			_image.x = 100;
			this.addChild(_image);
		}
		
		public function open(e:MouseEvent=null):void {
			this.dispatchEvent(new TrakUIEvent(TrakUIEvent.OPEN));
			this.__openned = true;
			
			this.close_mc.removeEventListener(MouseEvent.CLICK, this.open);
			this.close_mc.buttonMode = false;
			this.close_mc.gotoAndStop("close");
			
			this.__tween = new Tween(this, "y", Exponential.easeOut, this.y, 30, 1, true);
			this.__tween.addEventListener(TweenEvent.MOTION_FINISH, this.openStep2);
		}
		
		protected function openStep2(e:TweenEvent):void {
			this.__tween.removeEventListener(TweenEvent.MOTION_FINISH, this.openStep2);
			this.__tween = new Tween(this, "x", Exponential.easeInOut, this.x, 26, 1, true);
			this.__tween.addEventListener(TweenEvent.MOTION_FINISH, this.readyToClose);
		}
		
		protected function readyToClose(e:TweenEvent):void {
			this.__tween.removeEventListener(TweenEvent.MOTION_FINISH, this.readyToClose);
			this.__tween = null;
			this.close_mc.addEventListener(MouseEvent.CLICK, this.close);
			this.close_mc.buttonMode = true;
		}
		
		
		
		public function get openned():Boolean {
			return this.__openned;
		}
		
		protected function setDisplay():void {
			
		}
		
		public function get sourceWidth():int {
			return int(this.source_mc.width);
		}
		
		public function get sourceHeight():int {
			return int(this.source_mc.height);
		}
		
		public function set trackingType(_val:String):void {
			this.options_mc.trackingType = _val;
			this.tabBar.selectedIndex = TrakTypes.getTypesIndex(_val);
		}

		public function set source(_source:Bitmap):void {
			if(this.source_mc.numChildren > 0) {
				for(var i:int=0; i<this.source_mc.numChildren; i++)
				this.source_mc.removeChild(this.source_mc.getChildAt(i));
			}
			this.source_mc.addChild(_source);
			if(this.__colorDetection) this.showColorDetection();
		}
		
		public function set colorDetection(_val:Boolean):void {
			this.__colorDetection = _val;
		}
		
		public function showColorDetection():void {
			if(this.__colorDetectionZone) {
				if(this.__colorDetectionZone.parent) {
					this.__colorDetectionZone.parent.removeChild(this.__colorDetectionZone);
				}
			}
			this.__colorDetectionZone = new Shape();
			this.__colorDetectionZone.graphics.lineStyle( 0, 0xFFFFFF );
			this.__colorDetectionZone.graphics.drawRect(this.source_mc.width - 50, this.source_mc.height - 50, 50,	50);
			this.__colorDetectionZone.graphics.endFill();
			
			this.source_mc.addChild(this.__colorDetectionZone);
			
			this.colorDetected = 0xff0000;
		}
		
		public function updateCountdown(_time:int):void {
			if(this.__colorCountdown) {
				if(this.__colorCountdown.parent) {
					this.__colorCountdown.parent.removeChild(this.__colorCountdown);
				}
			}
			
			if(_time>0) {
			
				var textFormat:TextFormat 	= new TextFormat();
					textFormat.color 		= 0xFFFFFF;					textFormat.size			= 30;
				this.__colorCountdown = new TextField();
				this.__colorCountdown.text = String(_time);
				this.__colorCountdown.selectable = false;				this.__colorCountdown.setTextFormat(textFormat);
				this.__colorCountdown.x = (this.source_mc.width - 35);				this.__colorCountdown.y = (this.source_mc.height - 45);
				this.source_mc.addChild(this.__colorCountdown);
			}
		}
		
		public function set colorDetected(_color:uint):void {
			if(this.__colorDetectionColor) {
				if(this.__colorDetectionColor.parent) {
					this.__colorDetectionColor.parent.removeChild(this.__colorDetectionColor);
				}
			}
			if(_color==0) return;
			this.__colorDetectionColor = new Shape();
			this.__colorDetectionColor.graphics.beginFill(_color, 1);			this.__colorDetectionColor.graphics.drawRect(0, this.source_mc.height-50, 50, 50);
			this.__colorDetectionColor.graphics.endFill();
			
			this.source_mc.addChild(this.__colorDetectionColor);
		}
		
		
		public function set output(_output:Bitmap):void {
			if(this.output_mc.numChildren > 0) {
				for(var i:int=0; i<this.output_mc.numChildren; i++)
				this.output_mc.removeChild(this.output_mc.getChildAt(i));
			}
			
			this.output_mc.addChild(_output);
			
			// A box to represent the activity area
			this.__bounds = new Shape();
			this.__bounds.x = _output.x;
			this.__bounds.y = _output.y;
			this.output_mc.addChild(this.__bounds);
		}
		
		public function updateUI(...arguments):void {
			this.__bounds.graphics.clear();
			this.__bounds.graphics.lineStyle( 0, 0xFFFFFF );
				
			if(arguments[0] is Rectangle) {
				this.__bounds.graphics.drawRect(arguments[0].x,	arguments[0].y, arguments[0].width,	arguments[0].height);
			} else if(arguments[0] is Array) {
				for(var i:int = 0; i<arguments[0].length; i++) {
					this.__bounds.graphics.drawRect(arguments[0][i].x,	arguments[0][i].y, arguments[0][i].width, arguments[0][i].height);
				}
			}
			this.__bounds.graphics.endFill();
		}
		
		public function drawRects(_rects:Array):void {
			
		}
		
		public function reset():void {
			this.options_mc.setCalibration();
			this.__bounds.graphics.clear();
		}
		
				
		
	}
}
